// ==UserScript==
// @name         猫国建造者
// @namespace    http://gitee.com/adogs/tampermonkey-scripts/
// @version      1.0.22
// @updateURL    https://gitee.com/adogs/tampermonkey-scripts/raw/master/%e7%8c%ab%e5%9b%bd%e5%bb%ba%e9%80%a0%e8%80%85%e5%b7%a5%e5%85%b7.js
// @downloadURL  https://gitee.com/adogs/tampermonkey-scripts/raw/master/%e7%8c%ab%e5%9b%bd%e5%bb%ba%e9%80%a0%e8%80%85%e5%b7%a5%e5%85%b7.js
// @description  try to take over the world!
// @author       adogs
// @match        https://lolitalibrary.com/maomao*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=0.3
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';
    window.addEventListener('load', function () {
        // 创建主悬浮按钮
        const floatBtn = document.createElement('button');
        floatBtn.id = 'floating-button';
        floatBtn.innerHTML = '⚡';
        floatBtn.style.cssText = `
            position: fixed;
            bottom: 30px;
            right: 30px;
            width: 60px;
            height: 60px;
            border-radius: 50%;
            background: linear-gradient(135deg, #6a11cb 0%, #2575fc 100%);
            color: white;
            border: none;
            font-size: 24px;
            cursor: pointer;
            box-shadow: 0 4px 15px rgba(0,0,0,0.2);
            z-index: 9999;
            transition: all 0.3s ease;
        `;
        // 添加悬停效果
        floatBtn.addEventListener('mouseenter', () => {
            floatBtn.style.transform = 'scale(1.1)';
            floatBtn.style.boxShadow = '0 6px 20px rgba(0,0,0,0.3)';
        });
        floatBtn.addEventListener('mouseleave', () => {
            floatBtn.style.transform = 'scale(1)';
            floatBtn.style.boxShadow = '0 4px 15px rgba(0,0,0,0.2)';
        });
        floatBtn.addEventListener('click', () => {
            if (actionButtons.style.display === 'flex') {
                actionButtons.style.display = 'none';
            } else {
                actionButtons.style.display = 'flex';
            }
        });
        document.body.appendChild(floatBtn);

        // 创建功能按钮容器
        const actionButtons = document.createElement('div');
        actionButtons.id = 'action-buttons';
        actionButtons.style.cssText = `
        position: fixed;
        bottom: 100px;
        right: 30px;
        display: none;
        flex-direction: column;
        gap: 10px;
        z-index: 10000;
        flex-wrap: wrap;
        height: 20%;
    `;
        document.body.appendChild(actionButtons);

        var timers = {
            autoClip: undefined,
            autoTransWood: undefined,
            autoObserveSky: undefined,
            autoHunt: undefined,
            autoTransSheep: undefined
        };

        // 功能按钮数据
        const buttons = [
            { icon: '🥬', title: '自动采集猫薄荷', id: 'auto-clip', func: autoClipAction, args: [timers] },
            { icon: '🪵', title: '自动精炼猫薄荷', id: 'auto-trans-wood', func: autoTransWoodAction, args: [timers] },
            { icon: '🌌', title: '自动观测天空', id: 'auto-observe-sky', func: autoObserveSkyAction, args: [timers] },
            { icon: '🤠', title: '自动打猎', id: 'auto-hunt', func: autoHuntAction, args: [timers] },
            { icon: '🐏', title: '自动转换羊皮纸', id: 'auto-trans-sheep', func: autoTransSheepAction, args: [timers] },
            { icon: '❌', title: '关闭所有功能', id: 'close-auto', func: closeAutoAction, args: [timers] },
            { icon: '补满所有资源', title: '补满所有资源', id: 'fix-resource', func: fixResourceAction, args: [] },
        ];

        // 创建功能按钮
        buttons.forEach((btn, index) => {
            const actionBtn = document.createElement('button');
            actionBtn.className = 'action-btn';
            actionBtn.innerHTML = btn.icon;
            actionBtn.title = btn.title;
            actionBtn.dataset.id = btn.id;
            actionBtn.style.setProperty('--i', index);
            actionButtons.appendChild(actionBtn);
            actionBtn.style.background = 'blue';
            actionBtn.addEventListener('click', function () {
                btn.func.apply(null, [this, ...btn.args])
            });
        });

        // 悬浮按钮点击事件

        function autoClipAction(btn, timers) {
            if (timers.autoClip === undefined) {
                btn.title = "关闭自动采集";
                btn.style.background = 'red';
                timers.autoClip = setInterval(() => {
                    var element = document.getElementsByClassName("btn nosel modern")[0];
                    if (element.innerText === "采集猫薄荷") {
                        element.click();
                    }
                    //console.log("click");
                }, 500);
            }
            else {
                clearInterval(timers.autoClip);
                timers.autoClip = undefined;
                btn.title = "自动采集猫薄荷";
                btn.style.background = 'blue';
            }
        }

        function autoTransWoodAction(btn, timers) {
            if (timers.autoTransWood === undefined) {
                btn.title = "关闭自动精炼";
                btn.style.background = 'red';
                timers.autoTransWood = setInterval(() => {
                    // 尝试点击左侧的木头转换按钮
                    var element = document.querySelector('div[title="木材"] ~ div>div[title="10%"]');
                    if (element !== null) {
                        element.click();
                    } else {
                        element = document.getElementsByClassName("bldGroupContainer")[0]?.getElementsByTagName("td")[1]?.getElementsByClassName("btn nosel modern")[0]?.getElementsByTagName("a")[0];
                        if (element !== undefined && element.style.display !== "none") {
                            element.click();
                        }
                    }
                    //console.log("click");
                }, 1000);
            }
            else {
                clearInterval(timers.autoTransWood);
                timers.autoTransWood = undefined;
                btn.title = "自动精炼猫薄荷";
                btn.style.background = 'blue';
            }
        }

        function autoObserveSkyAction(btn, timers) {
            if (timers.autoObserveSky === undefined) {
                btn.title = "关闭自动观测";
                btn.style.background = 'red';
                timers.autoObserveSky = setInterval(() => {
                    var element = document.querySelector("#observeBtn");
                    if (element != undefined) {
                        element.click();
                    }
                    //console.log("click");
                }, 1000);
            }
            else {
                clearInterval(timers.autoObserveSky);
                timers.autoObserveSky = undefined;
                btn.title = "自动观测天空";
                btn.style.background = 'blue';
            }
        }

        function autoHuntAction(btn, timers) {
            if (timers.autoHunt === undefined) {
                btn.title = "关闭自动打猎";
                btn.style.background = 'red';
                timers.autoHunt = setInterval(() => {
                    var element = document.querySelector("#fastHuntContainer");
                    if (element != undefined && element.style.visibility === 'visible') {
                        element.querySelector("a").click();
                    }
                    //console.log("click");
                }, 1000);
            }
            else {
                clearInterval(timers.autoHunt);
                timers.autoHunt = undefined;
                btn.title = "自动打猎";
                btn.style.background = 'blue';
            }
        }

        function autoTransSheepAction(btn, timers) {
            if (timers.autoTransSheep === undefined) {
                btn.title = "关闭自动转换羊皮纸";
                btn.style.background = 'red';
                timers.autoTransSheep = setInterval(() => {
                    var element = document.querySelector('div[title="羊皮纸"] ~ div>div[title="1%"]');
                    if (element !== null) {
                        element.click();
                    }
                    //console.log("click");
                }, 1000);
            }
            else {
                clearInterval(timers.autoTransSheep);
                timers.autoTransSheep = undefined;
                btn.title = "自动转换羊皮纸";
                btn.style.background = 'blue';
            }
        }

        function fixResourceAction(btn) {
            Object.values(gamePage.resPool.resourceMap).forEach((resource) => {
                if (resource.unlocked === true && resource.visible === true && resource.maxValue != 0) {
                    resource.value = resource.maxValue;
                }
            })
        }

        function closeAutoAction(btn, timers) {
            Object.keys(timers).forEach(key => {
                if (timers[key] === undefined && key.includes("auto")) {
                    timers[key] = -1;
                }
            });
            buttons.forEach((btn, index) => {
                if (btn.id.includes("auto-")) {
                    var element = document.querySelector(`button[data-id="${btn.id}"]`);
                    btn.func.apply(null, [element, timers]);
                }
            });
        }
    });
})();